﻿using Mido.Domain;
using Mido.UiModules.SettingsModule;
using Mido.Utils.Mvvm;
using System;
using System.Windows;

namespace Mido.UiModules.ActiveImageModule
{
    public class ActiveImageViewModel : ViewModelBase
    {
        private readonly ActiveImageModel _model;

        internal ActiveImageViewModel(ActiveImageModel model)
        {
            if (model == null) 
                throw new ArgumentNullException("model");

            _model = model;
			_model.ActiveImageChanged += OnActiveImageChanged;
            _model.PositionInCollectionChanged += OnPositionInCollectionChanged;
        }

        private void OnPositionInCollectionChanged()
        {
            RaisePropertyChanged("NextImageVisibility");
            RaisePropertyChanged("PrevImageVisibility");
        }
		
        public SettingsModel Settings
        {
            get { return _model.Settings; }
        }

        public SimpleCommand ShowPrivCommand
        {
            get { return new SimpleCommand(_model.OnShowPriv); }
        }

        public SimpleCommand ShowNextCommand
        {
            get { return new SimpleCommand(_model.OnShowNext); }
        }

        public SimpleCommand ClearCustomPositionCommand
        {
            get { return new SimpleCommand(_model.OnClearCustomPosition); }
        }

        public SimpleCommand RotateRightCommand
        {
            get { return new SimpleCommand(_model.OnRotateRight); }
        }

		public SimpleCommand ExcludeCommand
        {
			get { return new SimpleCommand(_model.OnExclude); }
        }

		public SimpleCommand RotateLeftCommand
        {
            get { return new SimpleCommand(_model.OnRotateLeft); }
        }

        public Visibility ClearCustomPositionCommandVisibility
        {
			get { return _model.ActiveImage != null && _model.ActiveImage.CustomPosition.HasValue ? Visibility.Visible : Visibility.Collapsed; }
        }

        private void OnActiveImageChanged(ProcessedImage oldValue, ProcessedImage newValue)
        {
            RaisePropertyChanged("ActiveImage");
			RaisePropertyChanged("ButtonVisibility");
            RaisePropertyChanged("ImageCommandsPanelVisibility");
            RaisePropertyChanged("ClearCustomPositionCommandVisibility");

            if (oldValue != null)
                oldValue.CustomPositionChanged -= OnCustomPositionChanged;
            if (newValue != null)
                newValue.CustomPositionChanged += OnCustomPositionChanged;
        }

        private void OnCustomPositionChanged()
        {
            RaisePropertyChanged("ClearCustomPositionCommandVisibility");
        }

	    public void SetActiveImage(ProcessedImage selectedImage)
	    {
		    _model.ActiveImage = selectedImage;
	    }

        public ProcessedImage ActiveImage
        {
            get { return _model.ActiveImage; }
        }

	    public Visibility NextImageVisibility
	    {
		    get
		    {
			    if (_model.ActiveImage == null)
					return Visibility.Collapsed;

                return _model.IsLastImage ? Visibility.Collapsed : Visibility.Visible;
		    }
	    }

	    public Visibility PrevImageVisibility
	    {
		    get
		    {
			    if (_model.ActiveImage == null)
					return Visibility.Collapsed;

                return _model.IsFirstImage ? Visibility.Collapsed : Visibility.Visible;
		    }
	    }

	    public Visibility ImageCommandsPanelVisibility
	    {
		    get { return _model.ActiveImage == null ? Visibility.Collapsed : Visibility.Visible; }
	    }

        public static ActiveImageViewModel Designer
        {
            get
            {
                ActiveImageModel model = new ActiveImageModel(new SettingsModel());
                ActiveImageViewModel viewModel = new ActiveImageViewModel(model);

                model.ActiveImage = new ProcessedImage(string.Empty, model.Settings);
                return viewModel;
            }
        }
    }
}
